from flask import request
from flask_restful import Resource
from api.models import Order, OrderDetail, Express
from api.order import order_api, order_blue
from api.utils.response import to_response
from api import db


@order_blue.route('/order_list', methods=['GET'])
def order_list():
    try:
        id = request.args.get('id')
        if id:
            order = Order.query.get(id)
            return to_response(200, data=order.to_dict(), msg='获取订单列表成功！')
        else:
            order_list = Order.query.all()
            return to_response(200, data=[order.to_dict() for order in order_list], msg='获取订单列表成功！')
    except Exception as e:
        return to_response(20000, msg='获取订单列表失败！')


@order_blue.route('/express')
def get_express():
    id = request.args.get('id')
    if id:
        express = Express.query.filter(Express.oid == id).order_by(Express.update_time.desc()).all()
        if express:
            return to_response(200, data=[e.to_dict() for e in express], msg='获取快递信息成功！')
        else:
            return to_response(10017)
    else:
        return to_response(10000)
